<?php


namespace App\Repositories;


use App\Excel\Export\HsCode as HsCodeExport;
use App\Models\HsCode as Model;
use LTools\Repositories\RepositoryAbstract;

class HsCode extends RepositoryAbstract
{
    /**
     * HsCode constructor.
     * @param Model $model
     */
    public function __construct(Model $model)
    {
        $this->model = $model;
    }

    /**
     * @param array $attributes
     * @param array $columns
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
     */
    public function search(array $attributes, array $columns = ['*'])
    {
        return $this->model->newQuery()->where($attributes)->limit(20)->get($columns);
    }

    /**
     * @param $code
     * @return HsCodeExport
     */
    public function template($code)
    {
        return new HsCodeExport($code);
    }

    /**
     * @param Model $model
     * @return array
     */
    public function templateHeaders(Model $model)
    {
        $element = $model->getAttribute('element');

        $units = [$model->un1->getAttribute('name')];
        if ($model->un2) {
            $units[] = $model->un2->getAttribute('name');
        }
        return array_merge(['产品SKU'], $element, $units);
    }
}
